import pygame
import lwpgt

class GameObject:
    """gameobject is a class for representing each object in the game e.g. player, trees, items and others"""
    def __init__(self, tile):
        """init game object"""
        self.tile=tile
        self.rect=pygame.Rect(0, 0, tile.width, tile.height)
        #if given filename then load this tile
            
    def GetTile(self):
        """get tile form gameobject"""
        return self.tile
    
    def SetTile(self, tile):
        """set tile on gameobject"""
        self.tile=tile
    
    def Draw(self):
        """redraw gameobject"""
        pass
    
    def MoveObject(self, offsetx, offsety):
        """move object by given offset"""
        self.rect.move_ip((offsetx, offsety))
    
    def GetScreenPosition(self):
        """get the screen coordinates from object coordinates"""
        return self.rect.center
    
    def SetPosition(self, positionx, positiony):
        """set object position using map coordinates"""
        self.rect.x=positionx*lwpgt.render2d.DEFAULT_TILE_WIDTH
        self.rect.y=positiony*lwpgt.render2d.DEFAULT_TILE_HEIGHT
    
    def InitObject(self, filename):
        """init game object by loading tile from file"""
        try:
            img=pygame.image.load(filename).convert()
            img.set_colorkey((255, 0, 255))
        except:
            print "cannot init Tile for GameObject"
        else:
            self.tile=lwpgt.render2d.Tile(img)
    
    def GetRect(self):
        """get pygame.Rect() from self.position and tile size"""
        return self.rect
